package com.finger.gulimall.product.web;

import com.finger.common.utils.R;
import com.finger.gulimall.product.service.SkuInfoService;
import com.finger.gulimall.product.vo.SkuInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

@Controller
public class ItemController {

    @Autowired
    private SkuInfoService skuInfoService;

    /**
     * 商品详情页的检索
     *
     * @param skuId 商品skuid
     * @param model
     * @return 用于详情页检索的数据
     */
    @GetMapping("{skuId}.html")
    public String searchItem(@PathVariable("skuId") Long skuId, Model model) throws ExecutionException, InterruptedException {

        // 检索商品详情页
        SkuInfoVo skuInfoVo = skuInfoService.item(skuId);
        model.addAttribute("data", skuInfoVo);
        return "item";
    }

    /**
     * 根据商品id查询商品最新价格
     * @param skus
     * @return
     */
    @ResponseBody
    @GetMapping("/getNewItemPriForCart")
    public R getNewItemPriceForCart(@RequestParam("skus") List<Long> skus){
        Map<Long,BigDecimal> map = skuInfoService.getNewItemPriceForCart(skus);
        return R.ok().setData(map);
    }
}
